package iface.aplicativo.apresentacao.converter;

import iface.aplicativo.integracao.dao.MenuDAO;
import iface.aplicativo.negocio.entidade.Menu;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import org.springframework.web.jsf.FacesContextUtils;

@FacesConverter(value = "itemMenuConverter")
public class ItemMenuConverter implements Converter {

	public Object getAsObject(FacesContext fc, UIComponent uic, String string) {

		MenuDAO menuDAO = (MenuDAO) FacesContextUtils.getWebApplicationContext(
				fc).getBean("menuDAO");

		Long id = Long.parseLong(string);

		Menu menu = menuDAO.obter(id);

		return menu;
	}

	public String getAsString(FacesContext fc, UIComponent uic, Object o) {
		Menu menu = new Menu();
		menu = (Menu) o;
		return String.valueOf(menu.getIdMenu());
	}

}